import tools from "../../../tools"
import hierParentModel from './hierParentModel'
import util from "../../util"

export default class treeWorkCenterModel extends hierParentModel {
  constructor(hierProperties) {
    super(hierProperties, treeWorkCenterModel)
    this.recordsTree = []
    this.addRoot()
  }
  addRoot() {
    if (!this.find('nodeType', 'root')) {
      this.hierProperties.content.push(this.rootTpl())

      // 预置固定节点
      this.hierProperties.content = this.hierProperties.content.concat(this.fixNodeTpl())
    }
    this.formatTree()
  }
  formatTree() {
    this.recordsTree =  util.formatTree(
      util.deepCopy(this.hierProperties.content),
      this.hierProperties.content.filter(item => item.nodeType === 'root'),
      false, 'nodeId'
    )
    return this.recordsTree
  }
  // 保存前, 格式化content数据
  formatContent() {
    if (this.hierProperties.content && this.hierProperties.content.length) {
      this.hierProperties.content = this.hierProperties.content.map((item, index) => {
        let nextItem = this.hierProperties.content[index +1]
        delete item.children
        let res = {
          ...item,
          nodeType: item.name === 'root' ? 'root' :
            !!nextItem && (nextItem.parentId === item.nodeId) && (item.nodeType !== 'folder') ? 'node' : item.nodeType
        }
        return res;
      })
    }
  }
  rootTpl() {
    return {
      nodeId: 'root',
      name: 'root',
      nodeType: 'root',
      parentId: '',
      properties: [],
      link:{},
    }
  }
  // hierProperties.content 中预置的固定节点
  fixNodeTpl() {
      return [
          { 
              ...treeWorkCenterModel.fixOneNodeTpl(), name: 'config',
              properties: [
                  { name: 'createRouting', type: 'Boolean', defautValue: true, comments: '' },
                  { name: 'condition', type: 'String', defautValue: '1', comments: '' },
                  { name: 'auth', type: 'String', defautValue: '', comments: '' },
                  { name: 'timeStamp', type: 'DateTime', defautValue: '', comments: '' },
              ]
          },
          { ...treeWorkCenterModel.fixOneNodeTpl(), name: 'material', },
          { ...treeWorkCenterModel.fixOneNodeTpl(), name: 'product', },
          { ...treeWorkCenterModel.fixOneNodeTpl(), name: 'resource', },
          { ...treeWorkCenterModel.fixOneNodeTpl(), name: 'standard', },
          { ...treeWorkCenterModel.fixOneNodeTpl(), name: 'workOrder', },
          { ...treeWorkCenterModel.fixOneNodeTpl(), name: 'template', },
          { ...treeWorkCenterModel.fixOneNodeTpl(), name: 'signal', },
      ]
  }
  fixNodeList() {
    return [

    ]
  }
  static fixOneNodeTpl() {
      return {
          name: '', 
          nodeId: tools.uuid(), 
          nodeType: 'folder', 
          parentId: 'root', 
          link: {},
          children: [],
          properties: [],
          // fixNode: true,
      }
  }
  static tpl() {
    return {
      type: 'tree',
      content: [
        // item
      ]
    }
  }
  static itemTpl(data = {}) {
    return {
      nodeId: tools.uuid(),
      parentId: '',
      nodeType: "list",  //['root', 'list', 'dictionary', 'leaf']
      name: '新增节点',
      properties: [{type:'String'}],
      statusProperties: [],
      link:{
      },
      ...data
    }
  }
}